package com.vlyman.weixin.utils;


import com.vlyman.weixin.support.MailContentTypeEnum;
import com.vlyman.weixin.support.MailSender;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;

/**
 * 类描述：
 *  异常处理工具类
 * @author chenjs
 * @date 2018/6/19
 */
public class ExceptionEmailUtil {

    public static void sendMailToAdmin(Exception e, String title){
        new MailSender(title, ExceptionEmailUtil.getStackTrace(e.fillInStackTrace()),
                MailContentTypeEnum.TEXT, null).send();
    }

    public static void sendMailToAdmin(String msg, String title){
        new MailSender(title, msg,
                MailContentTypeEnum.TEXT, null).send();
    }

    public static void sendMailToAdmin(Exception e, String title, List<String> targets){
        new MailSender(title, ExceptionEmailUtil.getStackTrace(e.fillInStackTrace()),
                MailContentTypeEnum.TEXT, targets).send();
    }

    public static void sendMailToAdmin(String msg, String title, List<String> targets){
        new MailSender(title, msg,
                MailContentTypeEnum.TEXT, targets).send();
    }

    public static void sendMailToAdmin(String sender, Exception e, String title, List<String> targets){
        new MailSender(sender, title, ExceptionEmailUtil.getStackTrace(e.fillInStackTrace()),
                MailContentTypeEnum.TEXT, targets).send();
    }

    public static void sendMailToAdmin(String sender, String msg, String title, List<String> targets){
        new MailSender(sender, title, msg,
                MailContentTypeEnum.TEXT, targets).send();
    }

    /**
     * 方法描述：
     *  获取详细的异常链信息--精准定位异常位置
     * @author chenjs
     * @param throwable
     * @return String
     * @date 2018/6/19
     */
    public static String getStackTrace(Throwable throwable) {
        final Writer result = new StringWriter();
        final PrintWriter printWriter = new PrintWriter(result);
        throwable.printStackTrace(printWriter);
        return result.toString();
    }
}
